package com.konne.nightmare.FastPublicOpinion.ui.information.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.konne.nightmare.FastPublicOpinion.Event;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpActivity;
import com.konne.nightmare.FastPublicOpinion.common.GlideEngine;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.AddDatabasePresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IAddDatabaseView;
import com.konne.nightmare.FastPublicOpinion.ui.AddDataHelper;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.LocalUploadDialog;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.ResourceTypeDialog;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.TimeSelDialog;
import com.konne.nightmare.FastPublicOpinion.utils.BarUtil;
import com.konne.nightmare.FastPublicOpinion.utils.GlideUtils;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.konne.nightmare.FastPublicOpinion.widget.DatabaseImportView;
import com.konne.nightmare.FastPublicOpinion.widget.PicShowView;
import com.konne.nightmare.FastPublicOpinion.widget.QJItemTitleView;
import com.konne.nightmare.FastPublicOpinion.widget.QJRadioGroup;
import com.konne.nightmare.FastPublicOpinion.widget.QJSwitch;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/2/10  Time:下午 3:31.
 * description: 添加资料库 .
 */
public class AddDatabaseAct extends BaseMvpActivity<IAddDatabaseView, AddDatabasePresenter> implements IAddDatabaseView, View.OnClickListener, QJItemTitleView.OnCheckedChangeListener {
    @BindView(R.id.importView)
    DatabaseImportView importView;
    @BindView(R.id.itv_timing_time)
    QJItemTitleView itv_timing_time;
    private AddDataHelper dataHelper;
    public static void start() {
        ActivityUtils.startActivity(AddDatabaseAct.class);
    }

    @Override
    protected AddDatabasePresenter createPresenter() {
        return new AddDatabasePresenter();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_add_database;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this, true);
        dataHelper=new AddDataHelper(this,findViewById(R.id.pic_show),findViewById(R.id.ll_data));

        importView.setDatabaseImportListener(new DatabaseImportView.DatabaseImportListener() {
            @Override
            public void databaseImport(int type,boolean isSel) {
                switch (type) {
                    case 0:
                        new LocalUploadDialog(AddDatabaseAct.this).setLocalUploadListener(new LocalUploadDialog.LocalUploadListener() {
                            @Override
                            public void localUpload(int type) {
                                dataHelper.showLocalUpload(type);
                            }
                        }).show();
                        break;
                    case 1:
                        ImportDatabaseAct.start(1);
                        break;
                    case 2:
                        findViewById(R.id.cl_input_link).setVisibility(isSel?View.VISIBLE:View.GONE);
                        break;
                }
            }
        });
        ((QJItemTitleView) findViewById(R.id.itv_send_class)).setOnCheckedChangeListener(this);
        ((QJItemTitleView) findViewById(R.id.itv_timing)).setOnCheckedChangeListener(this);
        ((QJItemTitleView) findViewById(R.id.itv_comment)).setOnCheckedChangeListener(this);

        findViewById(R.id.itv_section).setOnClickListener(this);
        findViewById(R.id.itv_type).setOnClickListener(this);
        itv_timing_time.setOnClickListener(this);
        ((QJRadioGroup) findViewById(R.id.radioGroup)).setOnCheckedChangeListener(new QJRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(QJRadioGroup var1, int var2) {
                findViewById(R.id.ll_type).setVisibility(var2 == 1 ? View.VISIBLE : View.GONE);
            }
        });
    }
    @Subscribe
    public void onEvent(Event.AddDataItem dataItem){
        dataHelper.addDataItem();
    }

    @Override
    public void onCheckedChanged(QJItemTitleView titleView, QJItemTitleView.Style style, boolean isChecked) {
        switch (titleView.getId()) {
            case R.id.itv_send_class:
                findViewById(R.id.ll_send_sent).setVisibility(isChecked ? View.VISIBLE : View.GONE);
                if (isChecked) {
                    mPresenter.showClass();
                }
                break;
            case R.id.itv_timing:
                itv_timing_time.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                break;
            case R.id.itv_comment:
                findViewById(R.id.itv_see_other_com).setVisibility(isChecked ? View.VISIBLE : View.GONE);
                findViewById(R.id.itv_del_com).setVisibility(isChecked ? View.VISIBLE : View.GONE);
                titleView.showLineView(isChecked);
                break;
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.itv_timing_time:
                new TimeSelDialog(this).setTimeSelectListener(new TimeSelDialog.TimeSelectListener() {
                    @Override
                    public void timeSelect(String str) {
                        TextView tv_sent_time = findViewById(R.id.tv_sent_time);
                        tv_sent_time.setText(str);
                    }
                }).show();
                break;
            case R.id.itv_section:
                ImportDatabaseAct.start(0);
                break;
            case R.id.itv_type:
                new ResourceTypeDialog(this).show();
                break;
        }
    }

    @Override
    public void showClass(List<String> classs) {
        ViewGroup ll_send_sent = findViewById(R.id.ll_send_sent);
        LinearLayout classCon = (LinearLayout) ll_send_sent.getChildAt(1);
        classCon.removeAllViews();
        for (int i = 0; i < classs.size(); i++) {
            classCon.addView(getClassItem(classs.get(i)));
        }

    }

    private View getClassItem(String cl) {
        QJItemTitleView titleView = new QJItemTitleView(this);
        titleView.setTitle(cl);
        titleView.setTitleSize(12);
        titleView.setTitleColor(0xff67788A);
        titleView.setStyle(QJItemTitleView.Style.check);
        ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(40));
        titleView.setLayoutParams(params);
        return titleView;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        dataHelper.onActivityResult(requestCode,resultCode,data);
    }




}
